package com.xuyunceng.xinlin.entity;

import java.util.Collection;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.Email;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Length;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Entity
@Table(name = "`user`")
public class User implements UserDetails{

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	@Length(min = 6, max = 28)
	@Column(unique = true, nullable = false)
	private String username;
	@Length(min = 2, max = 16)
	private String nickname;
	@Column(length = 11 ,nullable = false)
	private String phone;
	@Email
	private String email;
	private String password; 
	@Column(length = 20)
	private String name; // 姓名
	private boolean gender; // 性别
	@Temporal(TemporalType.DATE)
	@Column(updatable = false)
	private Date birthday; // 生日
	@Size(max = 10, min = 0)
	private short charm; // 魅力指数
	@Length(min = 2, max = 10)
	private String home; // 来自何方

	@ManyToOne
	@JoinColumn(name = "perId")
	private Personality per; // 人格类型

	@ManyToOne
	@JoinColumn(name = "roleId")
	private Role role;
	
//	@ManyToOne
//	@JoinColumn(name="interestId") 
//	private Interest interest;	//	职业兴趣

	@Transient
	private String content;

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Personality getPer() {
		return per;
	}

	public void setPer(Personality per) {
		this.per = per;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isGender() {
		return gender;
	}

	public void setGender(boolean gender) {
		this.gender = gender;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public short getCharm() {
		return charm;
	}

	public void setCharm(short charm) {
		this.charm = charm;
	}

	public String getHome() {
		return home;
	}

	public void setHome(String home) {
		this.home = home;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public User() {
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		// 这个指用户的权限信息
		return null;
	}

	@Override
	public boolean isAccountNonExpired() {
		if (role.getId()!=4) {
			return true;
		}else {			
			return false;
		}
	}

	@Override
	public boolean isAccountNonLocked() {
		if (role.getId()!=5) {
			return true;
		}else {			
			return false;
		}
	}

	@Override
	public boolean isCredentialsNonExpired() {
		if (role.getId()!=6) {
			return true;
		}else {			
			return false;
		}
	}

	@Override
	public boolean isEnabled() {
		if (role.getId()!=7) {
			return true;
		}else {			
			return false;
		}
	}
}
